$(function() {
	question_list.initial();
});

var question_list = {
	initial : function initial() {
		this.bindChangeSearchParam();
		this.bindDelete();
		this.bindUpdate();
		this.bindUpdateField();
	},
	
	bindChangeSearchParam : function bindChangeSearchParam(){
		$(".pagination li a").click(function(){
			var pageId = $(this).data("id");
			if(pageId==null||pageId=="")return false;
			var genrateParamOld = question_list.genrateParamOld();
			genrateParamOld.page = pageId;
			question_list.redirectUrl(genrateParamOld);
			
		});
	},
	
	genrateParamOld :function genrateParamOld(){
		
		var page = 1;
		var data = new Object();
		data.page = page;
		
		return data;
	},

	redirectUrl : function(newparam) {
		var paramurl = newparam.page;
		document.location.href = document.getElementsByTagName('base')[0].href
				+ 'admin/field-list-' + paramurl;
	},
	bindDelete : function bindDelete(){
		$(".delete-btn").click(function(){
			var result = confirm("确认删除吗？此操作将会连同题库下关联的知识点和试题一并删除");
			if(result == true){
				$.ajax({
					headers : {
						'Accept' : 'application/json',
						'Content-Type' : 'application/json'
					},
					type : "GET",
					url : "admin/delete-field-" + $(this).data("id"),
					success : function(message, tst, jqXHR) {
						if (!util.checkSessionOut(jqXHR))
							return false;
						if (message.result == "success") {
							util.success("删除成功", function(){
								window.location.reload();
							});
						} else {
							util.error("操作失败请稍后尝试:" + message.result);
						}
					},
					error : function(jqXHR, textStatus) {
						util.error("操作失败请稍后尝试");
					}
				});
			}
			return false;
		});
	},
	bindUpdate : function bindUpdate(){
		$(".update-btn").click(function(){
			var field_id = $(this).data("id");
			$("#change-property-modal").modal({backdrop:true,keyboard:true});
			var tr = $(this).parent().parent();
			var field_name = tr.find(".td-field-name").text();
			var field_memo = tr.find(".td-field-memo").text();
			//var field_id =  $(this).parent().parent().find(":checkbox").val();
			$(".add-update-fieldname input").val(field_name);
			$(".add-update-memo input").val(field_memo);
			$("#add-update-fieldid").text(field_id);
		});
	},
	bindUpdateField : function bindUpdateField(){
		$("#update-field-btn").click(function(){
			var verify_result = question_list.verifyInput();
			var field_id = $("#add-update-fieldid").text();
			if (verify_result) {
				var data = new Object();
				data.fieldId = field_id;
				data.fieldName = $(".add-update-fieldname input").val();
				data.memo = $(".add-update-memo input").val();
				$.ajax({
					headers : {
						'Accept' : 'application/json',
						'Content-Type' : 'application/json'
					},
					type : "POST",
					url : "admin/field-update",
					data : JSON.stringify(data),
					success : function(message, tst, jqXHR) {
						console.log("修改题库返回："+message);
						if (!util.checkSessionOut(jqXHR))
							return false;
						if (message.result == "success") {
							util.success("修改成功", function(){
								window.location.reload();
							});
						} else {
							util.error(message.result);
						}

					},
					error : function(jqXHR, textStatus) {
						util.error("网络错误，请稍后尝试！");
					}
				});
			}
		});
	},
	verifyInput : function verifyInput() {
		$(".form-message").empty();
		$(".has-error").removeClass("has-error");
		var result = true;
		var r_checkName = question_list.checkName();
		var r_checkMemo = question_list.checkMemo();
		result = r_checkName && r_checkMemo;
		return result;
	},	
	checkName : function checkName() {
		var name = $(".add-update-fieldname input").val();
		if (name == "") {
			$(".add-update-fieldname .form-message").text("请输入题库名称");
			$(".add-update-fieldname input").focus();
			$(".add-update-fieldname input").addClass("has-error");
			return false;
		} else if (name.length > 40 || name.length < 2) {
			$(".add-update-fieldname .form-message").text("请保持在2-40个字符以内");
			$(".add-update-fieldname input").focus();
			$(".add-update-fieldname input").addClass("has-error");
			return false;
		} else {
			return true;
		}
	},
	
	checkMemo : function checkMemo() {
		var memo = $(".add-update-memo input").val();
		if (memo == "") {
			$(".add-update-memo .form-message").text("描述不能为空");
			$(".add-update-memo input").addClass("has-error");
			return false;
		} else if (memo.length > 40 || memo.length < 2) {
			$(".add-update-memo .form-message").text("请保持在2-40个字符以内");
			$(".add-update-memo input").addClass("has-error");
			return false;
		} 
		return true;
	}
};